package cn.android.support.v7.lib.sin.crown.dialog;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;

import cn.android.support.v7.lib.sin.crown.R;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;


/**
 * 加载界面【启动界面】
 * Created by 彭治铭 on 2017/5/21.
 */

public class GuidDailog extends BaseDialog {
    public static boolean isAttach = true;
    public static GuidDailog guidDailog;
    boolean locked;

    //传人需要显示的Bitmap位图, locked true屏蔽返回按键。
    //2秒后后自动消失。
    public static GuidDailog getInstance(Activity activity, Bitmap bitmap, boolean locked) {
        if (isfinish(activity,guidDailog)) {
            guidDailog = new GuidDailog(activity, bitmap);
        }
        guidDailog.locked = locked;
        return guidDailog;
    }

    private GuidDailog(Activity activity, Bitmap bitmap) {
        this.bitmap = bitmap;
        init(activity, R.layout.crown_dialog_guid);
    }

    View view;
    Bitmap bitmap;

    @Override
    protected void setWindowAnimations() {
        //super.setWindowAnimations();
        window.setWindowAnimations(R.style.crown_window_alpha);
    }

    @Override
    protected void initUI() {
        view = findViewById(R.id.crown_guid_dialog);
    }

    @Override
    protected void adapterUI() {
        ProportionUtils.getInstance().adapterScreen(activity, view);
    }

    @Override
    protected boolean isAutoAdapterUI() {
        return false;
    }

    @Override
    protected boolean isStatus() {
        return true;
    }

    @Override
    protected boolean isTransparent() {
        return false;
    }

    @Override
    protected boolean isLocked() {
        return locked;
    }

    @Override
    protected void listener() {
        isAttach = true;
        view = findViewById(R.id.crown_guid_dialog);
        if (bitmap != null) {
            view.setBackground(new BitmapDrawable(bitmap));
        }
        if (view != null && guidDailog != null) {
            view.postDelayed(new Runnable() {
                @Override
                public void run() {
                    guidDailog.dismiss();
                }
            }, 2000);//启动页2秒后自动消失
        }
    }

    @Override
    protected void recycleView() {
        isAttach = false;
        if (bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
        view.setBackground(null);
        view = null;
        System.gc();
    }

    public static void setIsAttach(boolean isAttach) {
        GuidDailog.isAttach = isAttach;
    }

    public static void setGuidDailog(GuidDailog guidDailog) {
        GuidDailog.guidDailog = guidDailog;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    public void setView(View view) {
        this.view = view;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }
}
